package org.linlinjava.litemall.admin.web;

import com.github.pagehelper.PageInfo;
import org.linlinjava.litemall.admin.annotation.LoginAdmin;
import org.linlinjava.litemall.admin.service.AdminGroupBuyService;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallGoods;
import org.linlinjava.litemall.db.domain.LitemallGroupBuy;
import org.linlinjava.litemall.db.domain.LitemallGroupBuyDetail;
import org.linlinjava.litemall.db.domain.LitemallGroupUser;
import org.linlinjava.litemall.db.service.LitemallGoodsService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhy
 * @date 2019-07-08 17:00
 **/
@RestController
@RequestMapping("/admin/group")
public class AdminGroupBuyController {
    @Resource
    private AdminGroupBuyService adminGroupBuyService;
    @Resource
    private LitemallGoodsService litemallGoodsService;

	/**
	 * 获取团购列表
	 * @param userId
	 * @param remark
	 * @param page
	 * @param limit
	 * @return
	 */
    @GetMapping("/getGroup")
    public Object getGroup(
	    @LoginAdmin Integer userId,
	    String remark,
	    @RequestParam(value = "page", defaultValue = "1") Integer page,
	    @RequestParam(value = "limit", defaultValue = "10") Integer limit){
	    if (userId == null) {
		    return ResponseUtil.unlogin();
	    }
	    List<LitemallGroupBuy> litemallGroupBuys = adminGroupBuyService.getGroupList(remark,page,limit);
	    long total = PageInfo.of(litemallGroupBuys).getTotal();
	    Map<String, Object> data = new HashMap<>(2);
	    data.put("total", total);
	    data.put("items", litemallGroupBuys);
	    return ResponseUtil.ok(data);
    }

	/**
	 * 添加团购列表
	 * @param userId
	 * @param litemallGroupBuy
	 * @return
	 */
	@PostMapping("/insertGroup")
	public Object insertGroup(
		@LoginAdmin Integer userId,
		@RequestBody LitemallGroupBuy litemallGroupBuy) {
		if (userId == null) {
			return ResponseUtil.unlogin();
		}
		// 校验此时间范围内是否有拼团计划
		int nums = adminGroupBuyService.findNum(litemallGroupBuy.getStartTime(),litemallGroupBuy.getEndTime());
		if(nums>0){
			return ResponseUtil.fail(403,"与其他活动时间有交集，请重新选择");
		}
		adminGroupBuyService.insertGroup(litemallGroupBuy);
		// 遍历goodIds
		String godIds = litemallGroupBuy.getGoodids();
		String[] god = godIds.split(",");
		LitemallGroupBuyDetail litemallGroupBuyDetail;
		for(String  godId:god){
			LitemallGoods litemallGoods = litemallGoodsService.findById(Integer.parseInt(godId));
			if(litemallGoods!=null) {
				litemallGroupBuyDetail = new LitemallGroupBuyDetail();
				litemallGroupBuyDetail.setGoodName(litemallGoods.getName());
				litemallGroupBuyDetail.setGoodId(Integer.valueOf(godId));
				litemallGroupBuyDetail.setLeastNum(litemallGroupBuy.getLeastNum());
				litemallGroupBuyDetail.setOrderLeftNum(litemallGroupBuy.getLeastNum());
				litemallGroupBuyDetail.setGroupId(litemallGroupBuy.getId());
				adminGroupBuyService.insertGroupDetail(litemallGroupBuyDetail);
			}
		}
		return ResponseUtil.ok(litemallGroupBuy);
	}

//	/**
//	 * 更新团购信息
//	 * @param userId
//	 * @param litemallGroupBuy
//	 * @return
//	 */
//	@PostMapping("/updateGroup")
//	public Object updateGroup(
//		@LoginAdmin Integer userId,
//		@RequestParam LitemallGroupBuy litemallGroupBuy){
//		if (userId == null) {
//			return ResponseUtil.unlogin();
//		}
//		return ResponseUtil.ok(adminGroupBuyService.updateGroup(litemallGroupBuy));
//	}


	/**
	 * 删除团购信息
	 * @param userId
	 * @param id
	 * @return
	 */
	@GetMapping("/deleteGroup")
	public Object deleteGroup(
		@LoginAdmin Integer userId,
		@RequestParam Integer id) {
		if (userId == null) {
			return ResponseUtil.unlogin();
		}
        int num = adminGroupBuyService.getDetailNumById(id);
		if(num>0){
			return ResponseUtil.fail(-1,"活动已开始，无法删除");
		}else {
			adminGroupBuyService.deleteGroup(id);
			return ResponseUtil.ok(adminGroupBuyService.deleteGroupDetailByGroupId(id));
		}
	}

	/**
	 * 获取团购详情
	 * @param userId
	 * @param id
	 * @param page
	 * @param limit
	 * @return
	 */
    @GetMapping("/getDetail")
	public Object getDetail(
	    @LoginAdmin Integer userId,
	    @RequestParam Integer id,
	    @RequestParam(value = "page", defaultValue = "1") Integer page,
	    @RequestParam(value = "limit", defaultValue = "10") Integer limit){
	    if (userId == null) {
		    return ResponseUtil.unlogin();
	    }
	    List<LitemallGroupBuyDetail> litemallGroupBuyDetails = adminGroupBuyService.getDetailList(id,page,limit);
	    long total = PageInfo.of(litemallGroupBuyDetails).getTotal();
	    Map<String, Object> data = new HashMap<>(2);
	    data.put("total", total);
	    data.put("items", litemallGroupBuyDetails);
	    return ResponseUtil.ok(data);
    }

	/**
	 * 更新团购商品数量
	 * @param userId
	 * @param id
	 * @param num
	 * @return
	 */
	@GetMapping("/updateDetail")
    public Object updateDetail(
	    @LoginAdmin Integer userId,
	    @RequestParam Integer groupId,
	    @RequestParam Integer id,
	    @RequestParam Integer num){
	    if (userId == null && groupId == null) {
		    return ResponseUtil.unlogin();
	    }
	    if(adminGroupBuyService.getDetail(groupId)<1){
	    	return ResponseUtil.fail(403,"只支持未开始的团购活动修改");
	    }
		adminGroupBuyService.updateGroupBuyDetail(id,num);
	    return ResponseUtil.ok();
    }

	/**
	 * 删除团购商品
	 * @param userId
	 * @param id
	 * @return
	 */
	@GetMapping("/deleteDetail")
	public Object deleteDetail(
	    @LoginAdmin Integer userId,
	    @RequestParam Integer id){
	    if (userId == null) {
		    return ResponseUtil.unlogin();
	    }
	    return ResponseUtil.ok(adminGroupBuyService.deleteGroupDetail(id));
    }

	/**
	 * 获取团购用户
	 * @param userId
	 * @param id
	 * @param page
	 * @param limit
	 * @return
	 */
	@GetMapping("/getGroupUser")
    public Object getGroupUser(
	    @LoginAdmin Integer userId,
	    @RequestParam Integer id,
	    @RequestParam(value = "page", defaultValue = "1") Integer page,
	    @RequestParam(value = "limit", defaultValue = "10") Integer limit){
	    if (userId == null) {
		    return ResponseUtil.unlogin();
	    }
	    List<LitemallGroupUser> litemallGroupUsers = adminGroupBuyService.getGroupUserList(id,page,limit);
	    long total = PageInfo.of(litemallGroupUsers).getTotal();
	    Map<String, Object> data = new HashMap<>(2);
	    data.put("total", total);
	    data.put("items", litemallGroupUsers);
	    return ResponseUtil.ok(data);
    }
}
